function ngdomBuilder() {
	if (!ngdom.Engine) return;
	this.engine = ngdom.Engine;
	
	this.inBuildMode = false;
	this.selectedWall = -1;
	this.wallsInit = false;
	this.rebuildWalls = function () {
		$('.debug-wall').remove();
		var walls = this.engine.map.grid[this.engine.map.position.x][this.engine.map.position.y].walls;
		for (var i = 0; i < walls.length; i++)
		{
			if (walls[i] > 0)
			{
				var wall = $('<div class="debug-wall"></div>')
					.attr('id', 'wall-'+i)
					//.html(walls[i])
					.addClass('wall-type-'+walls[i])
					.data('index', i)
					.css('left', ((i % ngdom.Engine.wallGridSize.width) * ngdom.Engine.wallGridScale)+'px')
					.css('top', (Math.floor(i / ngdom.Engine.wallGridSize.width) * ngdom.Engine.wallGridScale)+'px')
					.click(function(e) {
						var engine = ngdom.Engine, index = $(this).data('index'); 
						if (e.ctrlKey) {
							engine.map.grid[engine.map.position.x][engine.map.position.y].walls[index] = 0;
							if (engine.builder.selectedWall == index) engine.builder.selectedWall = -1;
							$(this).remove();
						}
						else {
							$('.debug-wall').removeClass('selected');
							$(this).addClass('selected');
							engine.builder.selectedWall = index;
						}
					})
					.appendTo(this.engine.portal);
				if (this.selectedWall == i) wall.addClass('selected');
			}
		}
	};
	this.keydown = function (e) {
		var engine = ngdom.Engine;

		if (e.which == 66 && e.shiftKey) //b
		{
			engine.pause = !engine.pause;
			engine.builder.inBuildMode = !engine.builder.inBuildMode;
		}

		if (!engine.builder.inBuildMode) return;

		if (e.which == 83) //s
		{
			if (typeof(engine.builder.saveTextArea) == 'undefined')
				engine.builder.saveTextArea = $('<textarea id="mapdata" readonly="readonly" wrap="off"></textarea>').appendTo('body');
			else
				engine.builder.saveTextArea.toggle();
			
			if (engine.builder.saveTextArea.is(':visible'))
			{
				var mapdata = engine.builder.generateMapFile();
				engine.builder.saveTextArea.val(mapdata).get(0).focus();
				engine.builder.saveTextArea.get(0).select();
			}
		}

		if (e.which == 69) //e
		{
			if (!engine.builder.enemyPaletteInit)
			{
				engine.builder.enemyPalette = $('<div id="enemy-palette"></div>').hide().appendTo(engine.deck);
				$.each(engine.enemyClasses, function (index, enemyClass) {
					eval('var enemy = new '+enemyClass+'(engine, 0, 0);');
					enemy.direction = 3;
					enemy.enterScene();
					enemy.el
						.remove()
						.appendTo(engine.builder.enemyPalette)
						.data('enemyClass', enemyClass)
						.click(function () {
							eval('var enemy = new '+$(this).data('enemyClass')+'(ngdom.Engine, \'random\', \'random\');');
							enemy.enterScene();
							ngdom.Engine.map.addObject(enemy);
						});
				});
				engine.builder.enemyPaletteInit = true;
			}
			engine.builder.enemyPalette.toggle();
		}

		if (e.which == 87) //w
		{
			if (!engine.builder.wallsInit)
			{
				engine.builder.rebuildWalls();
				engine.builder.wallsInit = true;
			}
			
			engine.portal.toggleClass('show-debug-walls');
		}

		if ((e.which == 8 || e.which == 46) && engine.builder.selectedWall >= 0) //backspace and delete
		{
			if (confirm('Delete the selected wall?'))
			{
				engine.map.grid[engine.map.position.x][engine.map.position.y].walls[engine.builder.selectedWall] = 0;
				$('#wall-'+engine.builder.selectedWall).remove();
				engine.builder.selectedWall = -1;
			}
			e.stopPropagation();
			return false; //prevent the browser from treating it as a back button
		}
	};
	this.keyup = function (e) {
	};
	this.switchScene = function () {
		$('.debug-wall').remove();
		this.wallsInit = false;
	};
	this.generateMapFile = function () {
		var newline = true;
		var mapfile = "{\r\n"
			+ "\tthis.size = { width: " + this.engine.map.size.width + ", height: " + this.engine.map.size.height + " };\r\n"
			+ "\tthis.position = { x: " + this.engine.map.initialPosition.x + ", y: " + this.engine.map.initialPosition.y + " };\r\n"
			+ "\tthis.imagePath = '" + this.engine.map.imagePath + "';\r\n"
			+ "\tthis.initGrid();\r\n";

		$.each(this.engine.map.grid, function (x, column) {
			//mapfile += "\tthis.grid["+x+"] = new Array();\r\n";
			$.each(column, function (y, scene) {
				if (newline) { mapfile += "\r\n"; newline = false; }
				//mapfile += "\tthis.grid["+x+"]["+y+"] = { img: new Image(), objects: [], walls: [] };\r\n";
				$.each(scene.objects, function (i, object) { newline = true;
					var init_x = isNaN(object.initialPosition.x) ? "'"+object.initialPosition.x+"'" : object.initialPosition.x;
					var init_y = isNaN(object.initialPosition.y) ? "'"+object.initialPosition.y+"'" : object.initialPosition.y;
					mapfile += "\tthis.grid["+x+"]["+y+"].objects["+i+"] = new "+ngdom.Utils.getObjectClass(object)+"(this.engine, "+init_x+", "+init_y+");\r\n";
				});
				$.each(scene.walls, function (i, wall) { newline = true;
					mapfile += "\tthis.grid["+x+"]["+y+"].walls["+i+"] = { left:"+wall.left+", top:"+wall.top+", right:"+wall.right+", bottom:"+wall.bottom+" };\r\n";
				});
			});
		});

		mapfile += "}";
		return mapfile;
	};
}